//
//  firstViewController.m
//  HumorousSript
//
//  Created by 李欢 on 15/6/25.
//  Copyright (c) 2015年 李欢. All rights reserved.
//

#import "firstViewController.h"
#import "SANetWorkingTask.h"
#import <UIImageView+WebCache.h>
#import "RecTableViewCell.h"

#define Width [UIScreen mainScreen].bounds.size.width
#define Height [UIScreen mainScreen].bounds.size.height


@interface firstViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray * tableArray;
@property (nonatomic, retain) UITableView * tableView;
@end

@implementation firstViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
        [self.view setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [SANetWorkingTask request:@"http://ic.snssdk.com/neihan/stream/category/data/v2/?tag=joke&iid=2808558815&min_time=1435197274&message_cursor=0&level=6&category_id=69&count=30&ac=WIFI&channel=tongbu&app_name=joke_essay&aid=7&version_code=4.1.0&device_platform=iphone&os_version=8.3&device_type=iPhone%205S&device_id=4272329077&vid=B06E994A-E093-479F-99E0-351D9340559F&openudid=e00e3962e37339ec7ce09708cad23b19b7a0693b&os_api=18&idfa=E2FD8DB1-FBA8-40EA-8C02-FAB446839AD1&screen_width=640" parmater:nil block:^(NSDictionary *result) {
        NSDictionary * dic = [result objectForKey:@"data"];
        NSMutableArray * array = [dic objectForKey:@"data"];
        self.tableArray = [NSMutableArray arrayWithArray:array];
        [self creatView];
        [self.tableView reloadData];
    
    }];
}

-(void)creatView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height ) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:self.tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    重定池
    static NSString * cellIdentify = @"cell";
    RecTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[RecTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
    }
    NSMutableDictionary * Dic = [self.tableArray objectAtIndex:indexPath.row];
    NSMutableDictionary * gDic = [Dic objectForKey:@"group"];
    NSMutableDictionary * uDic = [gDic objectForKey:@"user"];
    cell.nameLabel.text = [uDic objectForKey:@"name"];
    [cell.imageview sd_setImageWithURL:[uDic objectForKey:@"avatar_url"] placeholderImage:nil];
    
    cell.wordsLabel.text = [gDic objectForKey:@"content"];
//    取图片
    NSMutableDictionary * mDic = [gDic objectForKey:@"middle_image"];
    NSMutableArray * arr = [mDic objectForKey:@"url_list"];
    NSMutableDictionary * sDic = [arr objectAtIndex:0];
    [cell.picView sd_setImageWithURL:[sDic objectForKey:@"url"] placeholderImage:nil];
    cell.y = [[mDic objectForKey:@"height"]integerValue];;
    [cell setAlabelFrame];
    
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary * gDic = [self.tableArray objectAtIndex:indexPath.row];
    NSMutableDictionary * sDic = [gDic objectForKey:@"group"];
    NSMutableDictionary * mDic = [sDic objectForKey:@"middle_image"];
    return 30/667.0*Height +  [[mDic objectForKey:@"height"] integerValue] + 50/667.0*Height;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
